package com.ygqh.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.ygqh.common.constants.Consts;
import com.ygqh.common.xml.ListsuffixResultSerializer;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * 签名工具类
 */
public class MapUtil {
    /**
     * 连接字符串
     *
     * @param object    对象
     * @param encoder   是否编码
     * @param lowerCase 是否转换小写
     * @return String
     */
    public static String toJoinString(Object object, boolean encoder, boolean lowerCase) {

        return toJoinString(object, encoder, lowerCase, "&", "sign");
    }

    public static String toJoinString(Object object, boolean encoder, boolean lowerCase, String joiner, String... ignoreKey) {
        Map<String, String> map = new HashMap<>(Consts.MAP_DEFAULT_SIZE);
        JSONObject obj;
        if (object instanceof String) {
            obj = JSONObject.parseObject((String) object);
        } else {

            obj = ListsuffixResultSerializer.serializeToJSON(object);
        }
        for (String key : obj.keySet()) {
            map.put(key, obj.getString(key));
        }
        return toJoinStr(map, encoder, lowerCase, joiner, ignoreKey);
    }

    public static Map<String, String> transfeToMap(Object object) {
        Map<String, String> map = new HashMap<>(Consts.MAP_DEFAULT_SIZE);
        JSONObject obj;
        if (object instanceof String) {
            obj = JSONObject.parseObject((String) object);
        } else {
            obj = ListsuffixResultSerializer.serializeToJSON(object);
        }
        for (String key : obj.keySet()) {
            Object o = obj.get(key);
            if (o == null) {
                continue;
            }
            String value;
            if (o instanceof Date) {
                value = DateUtils.formatDateTime(obj.getDate(key));
            } else {
                value = obj.getString(key);
            }
            map.put(key, value);
        }
        return map;
    }

    public static String toJoinStr(Map<String, String> map, boolean encoder, boolean lowerCase) {
        return toJoinStr(map, encoder, lowerCase, "&", "sign");
    }

    /**
     * 拼接字符串
     *
     * @param map       对象
     * @param encoder   是否编码 针对key
     * @param lowerCase 是否转换小写 针对值
     * @return String
     */
    public static String toJoinStr(Map<String, String> map, boolean encoder, boolean lowerCase, String joiner, String... ignoreKey) {
        if (ignoreKey != null) {
            for (String key : ignoreKey) {
                map.remove(key);
            }
        }
        map = new TreeMap<>(map);
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, String>> set = map.entrySet();
        try {
            if (encoder && lowerCase) {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(entry.getKey().toLowerCase()).append("=").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append(joiner);
                }
            } else if (encoder) {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append(joiner);
                }
            } else if (lowerCase) {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(entry.getKey().toLowerCase()).append("=").append(entry.getValue()).append(joiner);
                }
            } else {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(entry.getKey()).append("=").append(entry.getValue()).append(joiner);
                }
            }
        } catch (UnsupportedEncodingException e) {
            ;
        }

        return sb.substring(0, sb.length() - joiner.length());
    }

    /**
     * 拼接字符串
     *
     * @param map       对象
     * @param encoder   是否编码
     * @param lowerCase 是否转换小写
     * @return String
     */
    public static String toJoinStrHasSize(Map<String, String> map, boolean encoder, boolean lowerCase) {
        map.remove("sign");
        map = new TreeMap<>(map);
        StringBuilder sb = new StringBuilder();
        Set<Map.Entry<String, String>> set = map.entrySet();
        try {
            if (encoder && lowerCase) {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey().toLowerCase()).append(":").append(getSize(entry.getKey(),
                            "value")).append("-").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append(";");
                }
            } else if (encoder) {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey()).append(":").append(getSize(entry.getValue(), "value"))
                            .append("-").append(URLEncoder.encode(entry.getValue(), Consts.UTF_8.name())).append(";");
                }
            } else if (lowerCase) {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey().toLowerCase()).append(":").append(getSize(entry.getValue(),
                            "value")).append("-").append(entry.getValue()).append(";");
                }
            } else {
                for (Map.Entry<String, String> entry : set) {
                    if (StringUtil.isBlank(entry.getValue())) {
                        continue;
                    }
                    sb.append(getSize(entry.getKey(), "key")).append("-").append(entry.getKey()).append(":").append(getSize(entry.getValue(), "value"))
                            .append("-").append(entry.getValue()).append(";");
                }
            }
        } catch (UnsupportedEncodingException e) {
            ;
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    private static String getSize(String str, String type) {
        return String.format("%04d", str.length());
    }

}
